package com.reservation.service;

import com.reservation.entity.Reservation;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 预订信息 业务逻辑接口
 */
@Service
public interface ReservationService {

    /**
     * 根据预订ID查询预订信息
     * @param rid 预订ID
     * @return 预订信息
     */
    @CircuitBreaker(name = "reservationService", fallbackMethod = "getReservationByIdFallback")
    Reservation getReservationById(Integer rid);

    /**
     * 查询所有预订信息
     * @return 预订信息列表
     */
    @CircuitBreaker(name = "reservationService", fallbackMethod = "getAllReservationsFallback")
    List<Reservation> getAllReservations();

    /**
     * 插入预订信息
     * @param reservation 预订信息
     * @return 操作结果
     */
    @CircuitBreaker(name = "reservationService", fallbackMethod = "insertReservationFallback")
    boolean insertReservation(Reservation reservation);

    /**
     * 更新预订信息
     * @param reservation 预订信息
     * @return 操作结果
     */
    @CircuitBreaker(name = "reservationService", fallbackMethod = "updateReservationFallback")
    boolean updateReservation(Reservation reservation);

    /**
     * 删除预订信息
     * @param rid 预订ID
     * @return 操作结果
     */
    @CircuitBreaker(name = "reservationService", fallbackMethod = "deleteReservationFallback")
    boolean deleteReservation(Integer rid);

    // 定义 fallback 方法（添加详细日志）
    default Reservation getReservationByIdFallback(Integer rid, Throwable t) {
        System.err.println("【Service熔断】根据ID查询预订降级：" + t.getClass().getName() + " - " + t.getMessage());
        t.printStackTrace();
        return null;
    }

    default List<Reservation> getAllReservationsFallback(Throwable t) {
        System.err.println("【Service熔断】查询所有预订降级：" + t.getClass().getName() + " - " + t.getMessage());
        return null;
    }

    default boolean insertReservationFallback(Reservation reservation, Throwable t) {
        System.err.println("【Service熔断】插入预订降级：" + t.getClass().getName() + " - " + t.getMessage());
        return false;
    }

    default boolean updateReservationFallback(Reservation reservation, Throwable t) {
        System.err.println("【Service熔断】更新预订降级：" + t.getClass().getName() + " - " + t.getMessage());
        return false;
    }

    default boolean deleteReservationFallback(Integer rid, Throwable t) {
        System.err.println("【Service熔断】删除预订降级：" + t.getClass().getName() + " - " + t.getMessage());
        return false;
    }
}